﻿<!--#INCLUDE FILE="config.asp"-->
<%
if(Session("AdminAccessLevel") <> 1 and Session("AdminAccessLevel") <> 3) then
	Response.Redirect("admin-login.asp?msg=invalid")
end if

Dim nameOfFile, output

if (request("filename") <> "0") then 
	nameOfFile = request("filename")
end if

if (request("guestID") <> "") then 
	guestID = request("guestID")
end if

dim count
	
	dim reservation, strItem
	SET reservation = CreateObject("Scripting.Dictionary")
	count = 0

sqlstring1 = ""
sqlstring1 = sqlstring1 & "SELECT DISTINCT Guests.*, Travels.*, a.AirlineName arrivalAriline, Transports.ApproximateArrivalTime,Transports.DepartureTime, b.AirlineName departureAirline, Codes.*, Reservation.ReservationCode,Reservation.ActualArrival,Reservation.ActualDeparture, Reservation.Status as rStatus "
sqlstring1 = sqlstring1 & "FROM Guests "
sqlstring1 = sqlstring1 & "LEFT JOIN Guests_Travels on Guests.GuestID = Guests_Travels.GuestID "
sqlstring1 = sqlstring1 & "LEFT JOIN Travels on Guests_Travels.TravelID = Travels.TravelID  "
sqlstring1 = sqlstring1 & "LEFT JOIN Reservation on Reservation.TravelId = Travels.TravelID  "
sqlstring1 = sqlstring1 & "LEFT JOIN Codes on Travels.CodeID = Codes.CodeID  "
sqlstring1 = sqlstring1 & "LEFT JOIN Transports on Guests.TransportID = Transports.TransportID "
sqlstring1 = sqlstring1 & "LEFT JOIN Airlines a on Transports.ArrivalAirlineID = a.AirlineID "
sqlstring1 = sqlstring1 & "LEFT JOIN Airlines b on Transports.DepartureAirlineID = b.AirlineID  "
sqlstring1 = sqlstring1 & "WHERE Guests.GuestID = "& guestID


set objConnect = server.createObject("ADODB.Connection")   

	objConnect.Open conStr

	set TblTemp = server.createobject("Adodb.Recordset")
	
	TblTemp.Open sqlstring1, objConnect
	
	Do Until TblTemp.EOF
		count = count + 1
		
		title = TblTemp("Title")
		name = TblTemp("Name")
		sureName = TblTemp("Surname")
		contactDate = TblTemp("CreatedDate")
		
		' transport
		arrivalAriline = TblTemp("arrivalAriline")
		arrivalTime = TblTemp("ApproximateArrivalTime")
		departureTime = TblTemp("DepartureTime")
		departureAirline = TblTemp("departureAirline")
		
		' Codes
		Code = TblTemp("Code")
		Cost = TblTemp("Cost")
		Denomination = TblTemp("Denomination")
		Tax = TblTemp("Tax")
		OfferDetails = TblTemp("OfferDetails")
		dim OffDetail
		OffDetail = replace(OfferDetails,"/",".<br/>- ")
		usernameAdmin = session("AdminName")

		' Reservation
		status = TblTemp("rStatus")
		ReservationCode = TblTemp("ReservationCode")
		actualArrival = TblTemp("ActualArrival")
		actualDeparture = TblTemp("ActualDeparture")
		
		' Travel
		numberOfGuests = TblTemp("TotalNumberGuests")
		PhoneNumber = TblTemp("PhoneNumber")
		
		reservation.Add ""&count, Array(ReservationCode,actualArrival,actualDeparture,numberOfGuests,status)
		
	TblTemp.MoveNext
	Loop
	
	' declare variable
	dim reservationDetails,reservationDetailsArriving
	
	reservationDetails = ""
	reservationDetailsArriving = ""
	Const ForReading = 1
	Const ForWriting = 2
	
	Dim strFileText
	
	filename = ""&nameOfFile&".txt"
	Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile(server.MapPath("Letter/"&filename),1)
	strFileText = objFileToRead.ReadAll()

	IF (filename = "Availability-English.txt") then
		' insert string
		For Each strItem In reservation
			If (reservation(strItem)("4") = "Pending Travel" and IsNull(reservation(strItem)("1")) = false ) then 
				reservationDetails = reservationDetails + "ReservationCode: "&reservation(strItem)("0")&"<br/> "
				reservationDetails = reservationDetails + "Reservation Dates: "& reservation(strItem)("1")&" - "& reservation(strItem)("2")&"<br/><br/> "
			end if
		Next

		
		objFileToRead.Close()
		' replace name
		if (title <> "") then
			strNewText = Replace(strFileText, "[title]", ""&Trim(title))
		end if
		if (name <> "") then
			strNewText = Replace(strNewText, "[first]", ""&name)
		end if
		
		if (sureName <> "") then
			strNewText = Replace(strNewText, "[last]", ""&sureName)
		end if
		' replace code
		if (Code <> "") then
			strNewText = Replace(strNewText, "[Codesofferdetails]", "Code: "&Code)
		end if	
		if (Cost <> "") then
			strNewText = Replace(strNewText, "[Codescost]", ""&Cost)
		end if
		
		if (Denomination <> "") then
			strNewText = Replace(strNewText, "[CodesDenomination]", ""&Denomination)
		end if
		
		if (Tax <> "") then
			strNewText = Replace(strNewText, "[codestax]", ""&Tax)
		end if
		
		' replace transport
		if (arrivalAriline <> "") then
			strNewText = Replace(strNewText, "[arrivalairline]", ""&arrivalAriline)
		end if
		if (arrivalTime <> "") then
			strNewText = Replace(strNewText, "[approximateArrival]", ""&arrivalTime)
		end if
		if (departureAirline <> "") then
			strNewText = Replace(strNewText, "[departureairline]", ""&departureAirline)
		end if
		if (departureAirline <> "") then
			strNewText = Replace(strNewText, "[approximatedeparture]", ""&departureTime)
		end if
		' reservation
		if (reservationDetails <> "") then
			strNewText = Replace(strNewText, "[ReservationDetail]", ""&reservationDetails)
		end if
		
	elseif (filename = "Fow-rsvsletter.txt") then
		' insert string
		For Each strItem In reservation
			If (reservation(strItem)("4") = "Pending Travel" and IsNull(reservation(strItem)("1")) = false) then
				reservationDetails = reservationDetails + "ReservationCode: "&reservation(strItem)("0")&", "&reservation(strItem)("3")&" invitados, llegando "&Month(reservation(strItem)("1"))&" "&Day(reservation(strItem)("1"))&", "&Year(reservation(strItem)("1"))&" "
				reservationDetails = reservationDetails + "y Salida "&Month(reservation(strItem)("2"))&" "&Day(reservation(strItem)("2"))&", "&Year(reservation(strItem)("2"))&". <br/> "
			end if
		Next

		
		objFileToRead.Close()
		' replace name
		if (title <> "") then
			strNewText = Replace(strFileText, "[suffix]", ""&Trim(title))
		end if
		if (sureName <> "") then
			strNewText = Replace(strNewText, "[lastname]", ""&sureName)
		end if 
		' replace code
		if (OfferDetails <> "") then
			strNewText = Replace(strNewText, "[offerdetailes]", ""&OffDetail)
		end if 
		if (Cost <> "") then
			strNewText = Replace(strNewText, "[travelcost]", ""&Cost)
		end if
		' replace transport
		if (usernameAdmin <> "") then
			strNewText = Replace(strNewText, "[admin]", ""&usernameAdmin)
		end if
		' reservation
		if(reservationDetails <> "") then
			strNewText = Replace(strNewText, "[rsvsdetails]", ""&reservationDetails)
		end if
		
	elseif (filename = "USA-RC-general-letterEN.txt") then
	
		objFileToRead.Close()
		' replace name
		if (title <> "") then
			strNewText = Replace(strFileText, "[suffix]", ""&Trim(title))
		end if
		if (sureName <> "") then
			strNewText = Replace(strNewText, "[Last Name]", ""&sureName)
		end if
		' replace code
		if (OfferDetails <> "") then
			strNewText = Replace(strNewText, "[offerdetails]", ""&OffDetail)
		end if
		if (Cost <> "") then
			strNewText = Replace(strNewText, "[cost]", ""&Cost)
		end if
		if (Tax <> "") then
			strNewText = Replace(strNewText, "[taxes]", ""&Tax)
		end if
		if (Denomination <> "") then
			strNewText = Replace(strNewText, "[denomination]", ""&Denomination)
		end if
		
		' replace transport
		if (usernameAdmin <> "") then
			strNewText = Replace(strNewText, "[admin]", ""&usernameAdmin)
		end if
		
		if (PhoneNumber <> "") then
			strNewText = Replace(strNewText, "[phonenumber]", ""&PhoneNumber)
		else 
			strNewText = Replace(strNewText, "[phonenumber]", "")
		end if
		
	elseif (filename = "CAN-FOWN-Vac-LetterEN.txt") then
		Dim i
		i = 0
		' insert string
		For Each strItem In reservation
			If (reservation(strItem)("4") = "Pending Travel" and IsNull(reservation(strItem)("1")) = false) then
				i = i + 1
				reservationDetails = reservationDetails + ""&i&". arriving: "&Month(reservation(strItem)("1"))&" "&Day(reservation(strItem)("1"))&", "&Year(reservation(strItem)("1"))&" "
				reservationDetails = reservationDetails + "and departing "&Month(reservation(strItem)("2"))&" "&Day(reservation(strItem)("2"))&", "&Year(reservation(strItem)("2"))&". <br/> "
			end if
		Next
	
		objFileToRead.Close()
		' replace name
		if (title <> "") then
			strNewText = Replace(strFileText, "[suffix]", ""&Trim(title))
		end if
		if (sureName <> "") then
			strNewText = Replace(strNewText, "[Last Name]", ""&sureName)
		end if
		
		' replace code
		if (OfferDetails <> "") then
			strNewText = Replace(strNewText, "[offerdetails]", ""&OffDetail)
		end if
		
		' reservation
		if (reservationDetails <> "") then
			strNewText = Replace(strNewText, "[Date]", ""&reservationDetails)
		end if

		' replace transport
		if (usernameAdmin <> "") then
			strNewText = Replace(strNewText, "[admin]", ""&usernameAdmin)
		end if
		
		' replace phone number
		if (PhoneNumber <> "") then
			strNewText = Replace(strNewText, "[phonenumber]", ""&PhoneNumber)
		else 
			strNewText = Replace(strNewText, "[phonenumber]", "")
		end if
		
	
	elseif (filename = "Follow-up_En.txt") then
	Dim x 
	x = 0
		' insert string
		For Each strItem In reservation
			if (IsNull(reservation(strItem)("1")) = false) then
				x = x + 1
				reservationDetailsArriving = reservationDetailsArriving + "arriving_"&x&": "&Month(reservation(strItem)("1"))&" "&Day(reservation(strItem)("1"))&", "&Year(reservation(strItem)("1"))&"; "
			end if
		Next
	
		objFileToRead.Close()
		' replace name
		if (title <> "") then
			strNewText = Replace(strFileText, "[suffix]", ""&Trim(title))
		end if
		if (sureName <> "") then
			strNewText = Replace(strNewText, "[last name]", ""&sureName)
		end if
		
		if (sureName <> "") then
			strNewText = Replace(strNewText, "[contact date]", ""&contactDate)
		end if
		
		if (Cost <> "") then
			strNewText = Replace(strNewText, "[cost]", ""&Cost)
		end if
		
		if (Tax <> "") then
			strNewText = Replace(strNewText, "[(taxes)]", ""&Tax)
		end if
		
		if (Denomination <> "") then
			strNewText = Replace(strNewText, "[denomination]", ""&Denomination)
		end if
		
		' replace code
		if (OfferDetails <> "") then
			strNewText = Replace(strNewText, "[offerdetails]", ""&OffDetail)
		end if
		
		' reservation
		if (reservationDetailsArriving <> "") then
			strNewText = Replace(strNewText, "[arriving]", ""&reservationDetailsArriving)
		end if

		' replace transport
		if (usernameAdmin <> "") then
			strNewText = Replace(strNewText, "[admin]", ""&usernameAdmin)
		end if
		
		' replace phone number
		if (PhoneNumber <> "") then
			strNewText = Replace(strNewText, "[phonenumber]", ""&PhoneNumber)
		else 
			strNewText = Replace(strNewText, "[phonenumber]", "")
		end if
		
	end if
	output = output + "<img src=""http://www.feifinancial.com/callcenter/images/costabaja_logo.jpg"" /><br/>"
	output = output + strNewText
	'output = output + "</textarea>"

	Response.Write output
%>